package com.rhino.xchat.ui.activity

import android.view.View
import com.blankj.utilcode.util.ActivityUtils
import com.blankj.utilcode.util.KeyboardUtils
import com.google.android.material.tabs.TabLayout
import com.google.android.material.tabs.TabLayout.OnTabSelectedListener
import com.rhino.base.extension.appInfo
import com.rhino.base.extension.toast
import com.rhino.base.extension.visible
import com.rhino.base.model.BaseModel.Companion.getViewModel
import com.rhino.base.ui.activity.BaseActionBarActivity
import com.rhino.xchat.R
import com.rhino.xchat.constant.RouteConstant
import com.rhino.xchat.databinding.ActivityLoginBinding
import com.rhino.xchat.viewmodel.AccountViewModel
import com.therouter.TheRouter
import com.therouter.router.Navigator
import com.therouter.router.Route
import com.therouter.router.interceptor.NavigationCallback


/**
 * @author LuoLin
 * @since Create on 2025/3/31.
 */
@Route(path = RouteConstant.LOGIN_ACTIVITY)
class LoginActivity : BaseActionBarActivity<ActivityLoginBinding>() {

    private val LOGIN_TYPE_SMS = 0
    private val LOGIN_TYPE_PWD = 1
    private var loginType: Int = LOGIN_TYPE_SMS

    private val accountViewModel by lazy {
        getViewModel(AccountViewModel::class.java)
    }

    override fun getLayoutResId(): Int {
        return R.layout.activity_login
    }

    override fun initTitle(): String? {
        return null
    }

    override fun initView() {
        dataBinding.onClickListener = this
        if (appInfo.isDebug) {
            dataBinding.inputPhone.setText("13000000000")
            dataBinding.inputSmsCode.setText("888888")
            dataBinding.inputPassword.setText("123456")
        }
        dataBinding.layoutSmsCode.visible(true)
        dataBinding.layoutInputPassword.visible(false)
        dataBinding.tabLayout.addOnTabSelectedListener(object : OnTabSelectedListener {
            override fun onTabSelected(tab: TabLayout.Tab) {
                loginType = tab.position
                when (loginType) {
                    LOGIN_TYPE_SMS -> {
                        dataBinding.layoutSmsCode.visible(true)
                        dataBinding.layoutInputPassword.visible(false)
                    }

                    LOGIN_TYPE_PWD -> {
                        dataBinding.layoutSmsCode.visible(false)
                        dataBinding.layoutInputPassword.visible(true)
                    }
                }
            }

            override fun onTabUnselected(tab: TabLayout.Tab) {
            }

            override fun onTabReselected(tab: TabLayout.Tab) {
            }
        })
    }

    override fun registerLiveData() {
        accountViewModel.statusLiveData.observe(this) { handleUiStatus(it) }
        accountViewModel.smsLoginLiveData.observe(this) {
            if (it != null) {
                TheRouter.build(RouteConstant.MAIN_ACTIVITY)
                    .navigation(callback = object : NavigationCallback() {
                        override fun onArrival(navigator: Navigator) {
                            ActivityUtils.finishActivity(WelcomeActivity::class.java)
                            ActivityUtils.finishActivity(LoginActivity::class.java)
                        }
                    })
            }
        }
        accountViewModel.pwdLoginLiveData.observe(this) {
            if (it != null) {
                TheRouter.build(RouteConstant.MAIN_ACTIVITY)
                    .navigation(callback = object : NavigationCallback() {
                        override fun onArrival(navigator: Navigator) {
                            ActivityUtils.finishActivity(WelcomeActivity::class.java)
                            ActivityUtils.finishActivity(LoginActivity::class.java)
                        }
                    })
            }
        }
    }

    override fun onSingleClick(view: View) {
        super.onSingleClick(view)
        when (view) {
            dataBinding.ivShowOrHidePassword -> {
                "todo".toast()
            }

            dataBinding.btnRegister -> {
                "todo".toast()
            }

            dataBinding.btnForgetPwd -> {
                "todo".toast()
            }

            dataBinding.btnWxLogin -> {
                "todo".toast()
            }

            dataBinding.btnQQLogin -> {
                "todo".toast()
            }

            dataBinding.ivAgreeUserPrivacy -> {
                "todo".toast()
            }

            dataBinding.btnLogin -> {
                KeyboardUtils.hideSoftInput(thisActivity)
                if (loginType == LOGIN_TYPE_SMS) {
                    if (dataBinding.inputPhone.text.isNullOrEmpty()) {
                        "请输入手机号".toast()
                        return
                    }
                    if (dataBinding.inputSmsCode.text.isNullOrEmpty()) {
                        "请输入验证码".toast()
                        return
                    }
                    accountViewModel.smsLogin(
                        dataBinding.inputPhone.text.toString(),
                        dataBinding.inputSmsCode.text.toString()
                    )
                } else {
                    if (dataBinding.inputPhone.text.isNullOrEmpty()) {
                        "请输入手机号".toast()
                        return
                    }
                    if (dataBinding.inputPassword.text.isNullOrEmpty()) {
                        "请输入密码".toast()
                        return
                    }
                    accountViewModel.pwdLogin(
                        dataBinding.inputPhone.text.toString(),
                        dataBinding.inputPassword.text.toString()
                    )
                }
            }
        }
    }
}